<?php
class Controller_Online_ProductImage extends Controller_Abstract
{
	function actionImageList()
	{
		$this->_view['title'] = '产品图片列表';
		$this->_view['export_channel_data'] = Helper_BSS_API::PMSCall('get_all_channel_info');
		if (! empty($_GET['pic_batch_number']))
		{
			$batch_prod_sku_array = Helper_BSS_API::PMSCall('get_one_batch', array('id' => trim($_GET['pic_batch_number'])));
			if (! empty($batch_prod_sku_array['skus']))
			{
				$_GET['product_number'] = $batch_prod_sku_array['skus'];
			}
		}
		$_GET['product_number'] = empty($_GET['product_number']) ? '' : trim($_GET['product_number']);
		$_GET['target_channel'] = empty($_GET['target_channel']) ? (empty($_COOKIE['target_channel']) ? 'default' : $_COOKIE['target_channel']) : $_GET['target_channel'];
		$_GET['file_type'] = empty($_GET['file_type']) ? (empty($_COOKIE['file_type']) ? false : $_COOKIE['file_type']) : $_GET['file_type'];
		if (! empty($_GET['product_number']))
		{
			$target_param = array(
				'c' => 'default',
				's' => $_GET['product_number']
			);
			$product_image_data = Helper_BSS_API::PMSCall('get_all_pic_target', $target_param);
			$export_url_data = '';
			if ($_GET['target_channel'] != 'default')
			{
				setcookie('target_channel', $_GET['target_channel'], time()+36000000, '/', $_SERVER['HTTP_HOST']);
				setcookie('file_type', $_GET['file_type'], time()+36000000, '/', $_SERVER['HTTP_HOST']);
				$target_param = array(
					'c' => $_GET['target_channel'],
					's' => $_GET['product_number']
				);
				$_product_image_data = Helper_BSS_API::PMSCall('get_all_pic_target', $target_param);
				$spilit_flag = ($_GET['file_type'] == 1) ? "\n" : "\t";
				if (empty($_GET['thumb_size']))
				{
					foreach ($_product_image_data as $k => $pid)
					{
						$export_url_data .= $k . $spilit_flag;
						foreach ($pid['pic'] as $key => $p)
						{
							$product_image_data[$k]['pic'][$key]['thumb']['url'] = $p['target'];
							$export_url_data .= $p['target'] . $spilit_flag;
						}
						$export_url_data .= "\n";
					}
				}
				else
				{
					foreach ($_product_image_data as $k => $pid)
					{
						$export_url_data .= $k . $spilit_flag;
						foreach ($pid['pic'] as $key => $p)
						{
							$url = empty($p['thumb']['800']) ? $p['target'] : $p['thumb']['800'];
							$product_image_data[$k]['pic'][$key]['thumb']['url'] = $url;
							$export_url_data .= $url . $spilit_flag;
						}
						$export_url_data .= "\n";
					}
				}
			}
			$this->_view['product_image_data'] = $product_image_data;
			$this->_view['export_url_data'] = $export_url_data;
		}
		else
		{
			$this->_view['product_image_data'] = array();
		}
	}
	
	function actionGetURLAjax()
	{
		$this->_view['title'] = '获取产品图片URL地址(Ajax)';
		if (! empty($_POST['product_number']) && ! empty($_POST['target_channel']))
		{
			setcookie('target_channel', $_POST['target_channel'], time()+36000000, '/', $_SERVER['HTTP_HOST']);
			$target_product_images_array = $_POST['product_number'];
			$target_param = array(
				'c' => $_POST['target_channel'],
				's' => implode(",", array_keys($target_product_images_array))
			);
			$export_images_url = Helper_BSS_API::PMSCall('get_all_pic_target', $target_param);
			if ($export_images_url)
			{
				$return_images_url_array = array();
				foreach ($export_images_url as $k => $eiu)
				{
					foreach($target_product_images_array[$k] as $v)
					{
						$return_images_url_array[$k][] = empty($_POST['thumb_size']) ? $eiu['pic'][$v]["target"] : (empty($eiu['pic'][$v]["thumb"]['800']) ? $eiu['pic'][$v]["target"] : $eiu['pic'][$v]["thumb"]['800']);
					}
				}
				echo json_encode($return_images_url_array); 
				exit();
			}
		}
	}
	
	function actionGetURLDetail()
	{
		$this->_view['title'] = '获取产品图片详细链接';
		if (! empty($_GET['product_number']))
		{
			$target_param = array(
				'sku' => $_GET['product_number'],
				'name' => $_GET['name'],
				'size' => $_GET['picture_size']
			);
			$result = Helper_BSS_API::PMSCall('get_all_pic_detail', $target_param);
			if (! $result)
			{
				return $this->_redirectMessage('获取失败', $_GET['product_number'] . '没有对应的图片', $_SERVER['HTTP_REFERER'], FAILURE);
			}
			$this->_view['result'] = $result;
		}
	}
}